﻿/* $RCSFile: CRMImageInit.cs $
 * Apache license v2 2011 by Christian Surieux
 * $Source: ?/AddonNice.ImageSettings/CRMImageInit.cs $
 * $log$
 * Revision 20 2011/01/21 17:51:08  christian.surieux
 *   Recover from Sony move files updated in 11/2010 were not in Source Control
 * Revision 7 2010/08/09 09:44:02  christian.surieux
 *   Added Log to cs sources files
 */

using System;
using System.Collections.Generic;
using System.Drawing.Imaging;
using System.Text;
using System.Diagnostics;
using System.Globalization;
using System.Reflection;

using AddonNice;
using AddonNice.UI.DataTypes;
using AddonNice.Configuration;
using AddonNice.Security;
using AddonNice.Diagnostics;
using AddonNice.Settings;
using AddonNice.Country;
using AddonNice.Helpers;

using AddonNice.CRMWrapper;

using Trace2    =   System.Diagnostics.Trace;

namespace AddonNice.CRMImage
{
	#region TraceSwitch for Crm Image
	public class CrmImageTrace
	{
		static public ADNTraceSwitch	Sw	=	new ADNTraceSwitch("CrmImageTrace","Trace Switch for Crm Image Module");
	}
	public class ImageMetaDataTrace
	{
		static public ADNTraceSwitch	Sw	=	new ADNTraceSwitch("ImageMetaDataTrace","Trace Switch for ImageMetaData class");
	}
	
	#endregion TraceSwitch Crm Image

	#region Install class for Package
	
	public class InstallPackage : IPackageInstaller
	{
		/// <summary>
		/// To call on package install
		/// </summary>
		public bool Install(CRMWrapperBase wrapper,int portalID)
		{
			// Nothing to do now
			return true;
		}
	}
	
	#endregion Install class for Package

	#region InitClass for package

	/// <summary>
	/// This class is dedicated to CRMImage package initialization: Cf the InitApplication class from ADCRM.Core
	/// It needs a declaration in web config
	/// Readen on Application start web.config loads the assembly containing thois class and instanciate the object in application object
	/// This allows installing various handlers and to complete the portal global settings with e-commerce specific settings
	/// </summary>
	public class CRMImageInit
	{
		// Insert the DynEntities in the static dictionnary to allow easy dynamic creation
		static CRMImageInit()
		{
			if ( CRMDynTraceSwitch.Sw.TraceVerbose )
				Trace.WriteLine( "[V]CRMImageInit static Ctor BEG.",CRMDynTraceSwitch.Sw.Info);
			Assembly asm            =   Assembly.GetExecutingAssembly();
			foreach (Type type in asm.GetTypes()) 
			{
			  if ( type.IsSubclassOf(typeof(DynEntity)) && type.IsClass ) 
			  {
				Type ntp            =   type.GetNestedType("Ftor",BindingFlags.NonPublic);
				bool isMiddle       =   (ntp == null );
				if ( CRMDynTraceSwitch.Sw.TraceVerbose )
					Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]CRMImageInit static Ctor found Name: {0}, Intermediary class: {1}.",type.Name, isMiddle),CRMDynTraceSwitch.Sw.Info);
				if ( isMiddle ) // no need to instanciate
					continue;
				ConstructorInfo ci  =   type.GetConstructor(Type.EmptyTypes);
				ci.Invoke(new Object[] { }) ; // just to force static member instanciation
				if ( CRMDynTraceSwitch.Sw.TraceVerbose )
					Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]DynEntity static Ctor after invoke Name: {0}.",type.Name),CRMDynTraceSwitch.Sw.Info);
			  }
			}
			if ( CRMDynTraceSwitch.Sw.TraceVerbose )
				Trace.WriteLine( "[V]CRMImageInit static Ctor END.",CRMDynTraceSwitch.Sw.Info);
		
		}


		public CRMImageInit()
		{
			if (CrmImageTrace.Sw.TraceVerbose)
				Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]CRMImageInit Ctor called at: {0}", DateTime.UtcNow.ToLongTimeString()), CrmImageTrace.Sw.Info);
			PortalSettingsDictionary.PortalSettingsCreatedEvent +=  new PortalSettingsCreatedEventHandler(PortalSettings_PortalSettingsCreatedEvent);
		}


		private void PortalSettings_PortalSettingsCreatedEvent(PortalSettingsCreatedEventArgs e)
		{
			if (CrmImageTrace.Sw.TraceVerbose)
				Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]CRMImageInit PortalSettings_PortalSettingsCreatedEvent BEG BaseSettings.Count: {0}", e.BaseSettings.Count), CrmImageTrace.Sw.Info);
			SettingsDictionary _baseSettings = e.BaseSettings;
			try
			{
				// We add the CrmImage Group to the SettingItemGroups.AllGroups static collection
				SettingItemGroup _Group = SettingItemGroup.CRM_IMAGE_SETTINGS;

				if (!SettingItemGroups.AllGroups.ContainsKey((int)_Group))
					SettingItemGroups.AllGroups.Add((int)_Group, ImStr.CRMImageSettings);

				#region Global path for image files
				int _groupOrderBase = (int)_Group;

				// Album Path Setting
				SettingItem AlbumPath           =   new SettingItem(ImStr.AlbumPath, new PortalUrlDataType());
				AlbumPath.Required              =   true;
				AlbumPath.Value                 =   "Album";
				AlbumPath.Group                 =   _Group;
				AlbumPath.Order                 =   _groupOrderBase;
				AlbumPath.ResourceFile          =   ImStr.ResourceFile;
				AlbumPath.ResourceItem          =   ImStr.ResourceItem;
				AlbumPath.EnglishName           =   "Images path";
				AlbumPath.Description           =   "Folder under current portal where all images files are physically stored.";
				_baseSettings.AddSetting(AlbumPath);

				ListDataType imgFmt             =   new ListDataType("Jpeg;Png");
				SettingItem ImgFormat           =   new SettingItem(ImStr.ImageFormat, imgFmt);
				ImgFormat.Required              =   true;
				ImgFormat.Value                 =   "Png";
				ImgFormat.Group                 =   _Group;
				ImgFormat.Order                 =   _groupOrderBase+1;
				ImgFormat.ResourceFile          =   ImStr.ResourceFile;
				ImgFormat.ResourceItem          =   ImStr.ResourceItem;
				ImgFormat.EnglishName           =   "Images format";
				ImgFormat.Description           =   "Image format used to save image files.";
				_baseSettings.AddSetting(ImgFormat);

				// Iframe Page Menu ID
				// Defaults to Image Menu but should have been changed in install for the created iFrame page
				SettingItem IFramePageMenu      =   new SettingItem(ImStr.IFramePageMenu, new IntegerDataType());
				IFramePageMenu.Required         =   true;
				IFramePageMenu.Value            =   StStr.PageImageManMenuID.ToString(); 
				IFramePageMenu.Group            =   _Group;
				IFramePageMenu.Order            =   _groupOrderBase+2;
				IFramePageMenu.ResourceFile     =   ImStr.ResourceFile;
				IFramePageMenu.ResourceItem     =   ImStr.ResourceItem;
				IFramePageMenu.EnglishName      =   "IFrame Page Menu";
				IFramePageMenu.Description      =   "Page menu dedicated to CrmImage Iframes.";
				_baseSettings.AddSetting(IFramePageMenu);

				#endregion

			}
			catch (Exception ex)
			{
				if (CrmImageTrace.Sw.TraceError)
					Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]CRMImageInit PortalSettings_PortalSettingsCreatedEvent ex:{0}", ex), CrmImageTrace.Sw.Info);
				throw;
			}
			if (CrmImageTrace.Sw.TraceVerbose)
				Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]CRMImageInit PortalSettings_PortalSettingsCreatedEvent END ok Count: {0}", e.BaseSettings.Count), CrmImageTrace.Sw.Info);
		}

	#endregion InitClass for package
	}
}